using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace SmilingGoat.SharePoint.WebParts
{
	/// <summary>
	/// Description of the toolpart. Override the GetToolParts method in your Web Part
	/// class to invoke this toolpart. To establish a reference to the Web Part 
	/// the user has selected, use the ParentToolPane.SelectedWebPart property.
	/// </summary>
	public class RssFeedReaderSettings: Microsoft.SharePoint.WebPartPages.ToolPart
	{
		TextBox txtInterval;
		TextBox urls;
		TextBox proxyAddress;
		TextBox proxyPort;
		DropDownList filterModes;
		CheckBox hideDescriptions;
        CheckBox expandDescriptions;
		CheckBox showDates;
		TextBox txtCache;

		/// <summary>
		/// Constructor for the class. A great place to set Set default values for
		/// additional base class properties here.
		/// <summary>
		public RssFeedReaderSettings()
		{
			   this.Title = "FeedReader Settings";
		}

		protected override void CreateChildControls()
		{
			RssFeedReader rss = (RssFeedReader)this.ParentToolPane.SelectedWebPart;

			Label l = new Label();
			l.Text = "RSS feed Urls";
			l.Font.Bold = true;

			Label l2 = new Label();
			l2.Text = "Separate RSS Urls with a ';'";
			l2.Font.Bold = false;

			urls = new TextBox();
			urls.CssClass = "ms-input";
			urls.TextMode = TextBoxMode.MultiLine;
			urls.Rows = 10;
			urls.Width = Unit.Percentage(100);
			if (rss.RssFeeds != null)
				urls.Text = rss.RssFeeds.Replace(";","; ");

			Label lblCache = new Label();
			txtCache = new TextBox();

			lblCache.Font.Bold = true;
			lblCache.Text = "<br>Cache Duration (mins)";
			
			txtCache.CssClass = "ms-input";
			txtCache.Width = Unit.Percentage(25);
			txtCache.Text = rss.CacheMinuteDuration;
			
			Label lblFilter = new Label();
			lblFilter.Font.Bold = true;
			lblFilter.Text = "<br>Filter Type";

			filterModes = new DropDownList();
			filterModes.CssClass = "ms-input";
			filterModes.Width = Unit.Pixel(100);
			filterModes.Items.Add("Days");
			filterModes.Items.Add("Items");
			filterModes.Items.Add("None");
			try
			{
				filterModes.Items.FindByText(rss.DisplayFilter.ToString()).Selected = true;
			}
			catch
			{
				filterModes.SelectedIndex = 1;
			}

			Label lblInterval = new Label();
			lblInterval.Font.Bold = true;
			lblInterval.Text = "<br>Filter Value";
			
			txtInterval = new TextBox();
			txtInterval.CssClass = "ms-input";
			txtInterval.Width = Unit.Pixel(75);
			txtInterval.Text = rss.FilterValue;

			showDates = new CheckBox();
			showDates.Text = "Show Dates";
			showDates.Checked = rss.ShowDates;

			hideDescriptions = new CheckBox();
			hideDescriptions.Text = "Hide Headline Descriptions";
			hideDescriptions.Checked = rss.HideDescriptions;

            expandDescriptions = new CheckBox();
            expandDescriptions.Text = "Expand Headline Descriptions by Default";
            expandDescriptions.ToolTip = "Controls whether headline descriptions are expanded when the page is first loaded, but only if headline descriptions are not hidden.";
            expandDescriptions.Checked = rss.ExpandDescriptions;

			Label lblProxy = new Label();
			lblProxy.Text = "<br>Proxy Server Address";
			
			proxyAddress = new TextBox();
			proxyAddress.CssClass = "ms-input";
			proxyAddress.Width = Unit.Percentage(100);
			proxyAddress.Text = rss.ProxyAddress;

			Label lblPort = new Label();
			lblPort.Text = "Proxy Server Port";

			proxyPort = new TextBox();
			proxyPort.CssClass = "ms-input";
			proxyPort.Width = Unit.Pixel(75);
			proxyPort.Text = rss.ProxyPort;

			// show version number
			Label v = new Label();
			v.Font.Bold = false;
			System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
			System.Reflection.AssemblyName an = a.GetName();
			RssFeedReader wp = (RssFeedReader)this.ParentToolPane.SelectedWebPart;

            string vs = an.Version.Major.ToString() + "." + an.Version.Minor.ToString() + "." + an.Version.Build.ToString() + "." + an.Version.Revision.ToString();
			v.Text = "<br/><br/><table border=0 cellpadding=2 cellspacing=0><tr><td valign=top><a href=\"http://timheuer.com/feedreader\" target=\"_blank\"><img alt=\"Tim Heuer logo\" src=\"" + wp.ResourcePath + "/thlogo-32.png\" alt=\"Tim Heuer - FeedReader " + vs.ToString() + "." + an.Version.Revision.ToString() + "\" border=0></a></td><td valign=top><nobr>FeadReader Version: " + vs + "</nobr><br/><nobr>Ms-Pl Licensed, 2008 <a href=\"http://timheuer.com/feedreader\" target=\"_blank\">Tim Heuer</a></nobr></td></tr></table>";

			Controls.Add(l);
			Controls.Add(l2);
			Controls.Add(urls);
			Controls.Add(lblCache);
			Controls.Add(txtCache);
			Controls.Add(lblFilter);
			Controls.Add(filterModes);
			Controls.Add(lblInterval);
			Controls.Add(txtInterval);
			Controls.Add(showDates);
			Controls.Add(hideDescriptions);
            Controls.Add(expandDescriptions);
			Controls.Add(lblProxy);
			Controls.Add(proxyAddress);
			Controls.Add(lblPort);
			Controls.Add(proxyPort);
			Controls.Add(v);
		}


		///	<summary>
		///	Called by the tool pane to apply property changes to the selected Web Part. 
		///	</summary>
		public override void ApplyChanges()
		{
			RssFeedReader ws = (RssFeedReader)this.ParentToolPane.SelectedWebPart;
			ws.RssFeeds = urls.Text.Replace(" ","");
			ws.ProxyAddress = proxyAddress.Text;
			ws.ProxyPort = proxyPort.Text;
			ws.HideDescriptions = hideDescriptions.Checked;
            ws.ExpandDescriptions = expandDescriptions.Checked;
			ws.ShowDates = showDates.Checked;
			ws.CacheMinuteDuration = txtCache.Text;
			ws.DisplayFilter = filterModes.SelectedValue.ToString();
			ws.FilterValue = txtInterval.Text;
			ws.ForceCacheRefreshSettings();
		}

		/// <summary>
		///	If the ApplyChanges method succeeds, this method is called by the tool pane 
		///	to refresh the specified property values in the toolpart user interface.
		/// </summary>
		public override void SyncChanges()
		{
		}
		
		/// <summary>
		///	Called by the tool pane if the user discards changes to the selected Web Part. 
		/// </summary>
		public override void CancelChanges()
		{
		}

		/// <summary>
		/// Render this Tool part to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void RenderToolPart(HtmlTextWriter output)
		{
			output.Write("<div style='padding:5px;width:100%'>");
			foreach (Control oControl in Controls)
			{
				oControl.RenderControl(output);
				output.Write("<br>");

			}	
			output.Write("</div>");
		}
	}											
}
								
